package com.ruoyi.web.controller.upload.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.FileDO;
import com.ruoyi.common.entity.VO.FileSearchVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.upload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/back/upload")
public class BackUploadController extends BaseController {
    private String prefix = "upload";

    @Autowired
    private IUploadService uploadService;

    @GetMapping("/{modelId}")
    public String upload(@PathVariable("modelId") Integer modelId, ModelMap mmap) {
        switch (modelId) {
            case 1:
                mmap.put("modelId", modelId);
                return prefix + "/imageList";
            default:
                return "";

        }
    }

    /**
     * 通用图片上传
     */
    @PostMapping("/image")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("modelType") Integer modelType) {
        try {
            if (!file.isEmpty()) {
                String createBy = ShiroUtils.getSysUser().getLoginName();
                FileDO fileDO = uploadService.addImageFile(file, modelType, createBy, ShiroUtils.getShopId());
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileDO.getFileName());
                ajax.put("url", fileDO.getUrl());
                return ajax;
            }
            return error("没有找到上传图片！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传图片失败！");
        }
    }

    /**
     * 插件图片上传
     */
    @PostMapping("/uploadImage")
    @ResponseBody
    public AjaxResult uploadImage(@RequestParam("file_data") MultipartFile file, @RequestParam("modelType") Integer modelType) {
        try {
            if (!file.isEmpty()) {
                String createBy = ShiroUtils.getSysUser().getLoginName();
                FileDO fileDO = uploadService.addImageFile(file, modelType, createBy, ShiroUtils.getShopId());
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileDO.getFileName());
                ajax.put("url", fileDO.getUrl());
                return ajax;
            }
            return error("没有找到上传图片！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传图片失败！");
        }
    }

    /**
     * 插件图片删除
     */
    @PostMapping("/deleteImage")
    @ResponseBody
    public AjaxResult deleteImage(@RequestParam("file_data") MultipartFile file, @RequestParam("modelType") Integer modelType) {
        try {
            if (!file.isEmpty()) {
                String createBy = ShiroUtils.getSysUser().getLoginName();
                FileDO fileDO = uploadService.addImageFile(file, modelType, createBy, ShiroUtils.getShopId());
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileDO.getFileName());
                ajax.put("url", fileDO.getUrl());
                return ajax;
            }
            return error("没有找到上传图片！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传图片失败！");
        }
    }

    /**
     * 上传微信支付证书到本地
     */
    @PostMapping("/cert")
    @ResponseBody
    public AjaxResult uploadCert(@RequestParam("file") MultipartFile file, @RequestParam("shopId") Long shopId) {
        if (ShiroUtils.getShopId() != 0 && !ShiroUtils.getShopId().equals(shopId)) {
            return error("没有权限上传该店铺的支付证书！");
        }
        try {
            if (!file.isEmpty()) {
                FileDO fileDO = uploadService.addCertFile(file, shopId);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileDO.getFileName());
                ajax.put("url", fileDO.getUrl());
                return ajax;
            }
            return error("没有找到支付证书！");
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return error("上传支付证书失败！");
        }
    }

    /**
     * 查询列表数据
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FileSearchVO fileSearchVO) {
        startPage();
        List<FileDO> list = uploadService.searchList(fileSearchVO);
        return getDataTable(list);
    }

}
